package com.example.controller;

import com.example.entity.LogOperationBig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Slf4j
public class IndexController {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Value("${name}")
    private String name;
    @Value("${name2}")
    private String name2;
    @Value("${name3}")
    private String name3;
    @Autowired
    private com.example.mapper.LogOperationBigMapper logOperationBigMapper;

    @RequestMapping
    public String index(){
        log.debug("name={}, name2={}, name3={}", name, name2, name3);
        return "index!!!";
    }

    @RequestMapping("selectLog")
    public LogOperationBig selectLog(@RequestParam Long id){
        LogOperationBig logOperationBig = logOperationBigMapper.selectById(id);
        return logOperationBig;
    }

    @RequestMapping(value = "/redis/{key}/{value}",method = RequestMethod.GET)
    @ResponseBody
    public String redisTest(@PathVariable String key, @PathVariable String value) {
        String redisValue = stringRedisTemplate.opsForValue().get(key);
        if (StringUtils.isEmpty(redisValue)) {
            stringRedisTemplate.opsForValue().set(key,value);
            return "操作成功！";
        }

        if (!redisValue.equals(value)) {
            stringRedisTemplate.opsForValue().set(key,value);
            return "操作成功！";
        }

        return String.format("redis中已存在[key=%s,value=%s]的数据！",key,value);
    }

}
